/* gui.cpp : contains implementation of qui components */

#include "gui.h"
#include "../logic/logic.h"

//temporary string. (static objects shared among all objects Cell
char tmp[BUF];
string DefaultName ("def.name");
MainWindow * mainWindow;

//-----------MainWindow---------------
/*Function: MainWindow
  draw function inside constructor*/
MainWindow::MainWindow ( QApplication *app)
{
    //save pointer to application
    appl = app;
    //main draw area
    window = new QWidget;
    setCentralWidget(window);
    
    grid = new Grid();

    createActions();
    createMenus();
    createStatusBar();

    button = new QPushButton ( "Exit");
    turnButton = new QPushButton ( "Turn");
    //layouts
    mainLayout = new QHBoxLayout;
    createRightPanel();
    mainLayout->addWidget ( grid); //left 
    mainLayout->addLayout ( rightLayout);//right
    //right
    window->setLayout( mainLayout);

    setWindowTitle(tr("Monopoly"));
    setWindowIcon(QIcon("./icon.png"));    

    QObject::connect(button, SIGNAL(clicked()), app, SLOT(quit()));
    QObject::connect(turnButton, SIGNAL(clicked()), this, SLOT(newTurn()));
};

void MainWindow :: createRightPanel()
{
    rightLayout = new QVBoxLayout;
    createTurnInfo();
    createObjectState();
    createPlayersBuilding();
    rightLayout->addWidget ( turnState);
    rightLayout->addWidget ( objectState);
    rightLayout->addWidget ( playersBuilding);

    rightLayout->addWidget ( turnButton);
    rightLayout->addWidget ( button);
    
    redrawObjectState();
    redrawTurnInfo();
}

/*Function: ~MainWindow
  just destructor*/
MainWindow :: ~MainWindow()
{
};

void MainWindow::createActions()
{
    //printf("create actions\n");
    newAction = new QAction(tr("&New"), this);
    newAction->setShortcut(tr("Ctrl+N"));
    newAction->setIcon(QIcon(":/images/new.png"));
    newAction->setStatusTip(tr("New game"));
    connect(newAction, SIGNAL(triggered()), this, SLOT(newGame()));

    exitAction = new QAction(tr("&Exit"), this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setIcon(QIcon(":/images/exit.png"));
    exitAction->setStatusTip(tr("Exit game"));
    connect(exitAction, SIGNAL(triggered()), this, SLOT(exitGame()));

    buyAction = new QAction(tr("&Buy"), this);

    aboutAction = new QAction(tr("About"), this);
    aboutAction->setStatusTip(tr("Info about application"));
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));

    showLineAction = new QAction(tr("&Show Lines"), this);
    showLineAction->setCheckable(true);
    showLineAction->setChecked(grid->getShowLines());
    showLineAction->setStatusTip(tr("Show or hide the field's"
    "lines"));
    connect(showLineAction, SIGNAL(toggled(bool)), grid, SLOT(setShowLines(bool)));
    connect(showLineAction, SIGNAL(toggled(bool)), grid, SLOT(update()));

}

void MainWindow :: createMenus()
{
    printf("create menus\n");
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(newAction);
    //separatorAction = fileMenu->addSeparator();
    fileMenu->addAction(exitAction);

    editMenu = menuBar()->addMenu(tr("&Edit"));
    editMenu->addAction(buyAction);
    editMenu->addAction(showLineAction);
    

    helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(aboutAction);
}   

/*void MainWindow::createContextMenu()
{
    grid->addAction(buyAction);
    grid->setContextMenuPolicy(Qt::ActionsContextMenu);
}
*/
void MainWindow::createStatusBar()
{
    statusLabel = new QLabel(" game running ");
    statusLabel->setAlignment(Qt::AlignHCenter);
    statusLabel->setMinimumSize(statusLabel->sizeHint());
    statusBar()->addWidget(statusLabel);
    updateStatusBar();
}
void MainWindow::updateStatusBar()
{
    tmp[0] = 0;
    sprintf( tmp,"game running: player %s", 
        mainSession->getCurrentPlayer()->getName());
    statusLabel->setText(tr(tmp));
}
void MainWindow::updateStatusBar( char *s)
{
	tmp[0] = 0;
	sprintf( tmp,"player %s%s", 
        mainSession->getCurrentPlayer()->getName(), s);
	statusLabel->setText( tr(tmp));
}

/*Function: newTurn
  new turn button emits this event*/
void MainWindow :: newTurn()
{
    mainSession->turn();
    mainWindow->redrawTurnInfo();
    mainWindow->redrawPlayersBuilding();
    mainWindow->updateStatusBar();
};

//----------Turn Info------------
void MainWindow :: createTurnInfo()
{
    //turnState
    turnState = new QGroupBox(tr("Turn State"));
    turnStateLayout = new QVBoxLayout;
    numOfTunrs = new QLabel ("0 turn");
    currentPlayer = new QLabel ("Default Player");
    playersMoney = new QLabel ("0 $");
    playersIncome = new QLabel ("0 $");
    turnStateLayout->addWidget(numOfTunrs);
    turnStateLayout->addWidget(currentPlayer);
    turnStateLayout->addWidget(playersMoney);
    turnStateLayout->addWidget(playersIncome);
    //temporary size layout
    QSizePolicy sp (QSizePolicy::Minimum, QSizePolicy::Minimum);
    turnState->setSizePolicy( sp);
    
    turnState->setLayout(turnStateLayout);
}

/*Function: redrawTurnInfo
  initialize redrawing of left part*/
void MainWindow :: redrawTurnInfo()
{
    Player * curPl = mainSession->getCurrentPlayer();
    if (curPl == NULL) //if Player == NoPlayer
        return;
    QString cPname ( curPl->getName() );
    char tmp[BUF];
    sprintf ( tmp, "money: %i$", curPl->getMoney() );
    QString cPmoney ( tmp);
    tmp[0]= 0; 
    sprintf ( tmp, "turn #%i", mainSession->getTurnCounter()  );
    QString cTurn ( tmp);
    tmp[0]= 0; 
    sprintf ( tmp, "income %d$", curPl->getIncome()  );
    QString cIncome ( tmp);
    
    //printf ( "redraw Turn, cp = %s\n", curPl->getName());
    currentPlayer->setText ( cPname);
    numOfTunrs->setText ( cTurn);
    playersMoney->setText ( cPmoney);
    playersIncome->setText ( cIncome);
};

//----------Object State------------
void MainWindow :: createObjectState()
{
    //obJect State
    objectState = new QGroupBox(tr("Object State"));
    objectStateLayout = new QVBoxLayout;
    objName = new QLabel ("noName");
    //objName->setMaximumSize(objName->sizeHint());
    objPrice = new QLabel ("0 $");
    objIncome = new QLabel ("income: 0 $");
    objOutcome = new QLabel ("outcome: 0 $");
    objOwner = new QLabel ("noName");
    
    //set consumption
    objStateCons = new QGroupBox(tr("consumtion:"));
    objStateConsLayout = new QVBoxLayout;
    objCons = new QLabel ("default res: 0");
    objStateConsLayout->addWidget(objCons);
    objStateCons->setLayout(objStateConsLayout);

    //set production
    objStateProd = new QGroupBox(tr("production:"));
    objStateProdLayout = new QVBoxLayout;
    objProd = new QLabel ("default res: 0");
    objStateProdLayout->addWidget(objProd);
    objStateProd->setLayout(objStateProdLayout);
    objType = new QLabel ("Notype");

    //set obj state
    objectStateLayout->addWidget(objName);
    objectStateLayout->addWidget(objType);
    objectStateLayout->addWidget(objIncome);
    objectStateLayout->addWidget(objOutcome);
    objectStateLayout->addWidget(objOwner);
    objectStateLayout->addWidget(objStateCons);
    objectStateLayout->addWidget(objStateProd);
    
    //temporary size layout
    QSizePolicy sp (QSizePolicy::Minimum, QSizePolicy::Minimum);
    objectState->setSizePolicy( sp);
    objectState->setLayout(objectStateLayout);
}
/*Function: redrawTurnInfo
  initialize redrawing of left part*/
void MainWindow :: redrawObjectState()
{
    Cell * cell = grid->curActCell;
    QString oName ( cell->getName());
    strcpy ( tmp, "");
    sprintf ( tmp, "%d $", cell->getPrice() );
    QString oPrice ( tmp);
    strcpy ( tmp, "");
    sprintf ( tmp, "%d $", cell->getIncome() );
    QString oIncome ( tmp);
    strcpy ( tmp, "");
    sprintf ( tmp, "owned by:\n%d", cell->getOwner());
    QString oOwner ( tmp);
    objIncome->setText ( oIncome);
    objPrice->setText ( oPrice);
    objName->setText ( oName);
    objOwner->setText ( oOwner);
    objType->setText ( tr(cell->getType()->typeName));
    tmp [0] = 0;
    sprintf( tmp, "%s : %d\n", cell->getConsRes()[0].getName(), 
        cell->getConsAmount() [0]);
    objCons->setText ( tr(tmp));
    tmp [0] = 0;
    sprintf( tmp, "%s : %d\n", cell->getProdRes()[0].getName(), 
        cell->getProdAmount() [0]);
    objProd->setText ( tr(tmp));
};

//----------Players Building------------
void MainWindow :: createPlayersBuilding()
{
    //obJect State
    playersBuilding = new QGroupBox(tr("Player Buildings"));
    playersBuildingLayout = new QVBoxLayout;
    //buildings.push_back( new QLabel ("dummy building"));
    //for(unsigned int i = 0; i < buildings.size(); i++)
    //    playersBuildingLayout->addWidget(buildings[i]);    
    
    //temporary size layout
    //playersBuilding->setMaximumHeight(130.0);
    playersBuilding->setUpdatesEnabled(true);
    playersBuilding->setLayout(playersBuildingLayout);
}

void MainWindow :: redrawPlayersBuilding()
{
    Player * curPl = mainSession->getCurrentPlayer();
    if (curPl == NULL) //if Player == NoPlayer
        return;
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //Temporaty, I hope, I hope, I hope!!!!!!
    rightLayout->removeWidget( playersBuilding);
    rightLayout->removeWidget( button);
    rightLayout->removeWidget( turnButton);
    delete playersBuildingLayout;
    delete playersBuilding;
    createPlayersBuilding();
    rightLayout->addWidget( playersBuilding);
    rightLayout->addWidget( turnButton);
    rightLayout->addWidget( button);
    

    vector <Building> vb = curPl->getBuilding();
    buildings.clear();
    for(unsigned int i = 0; i < vb.size(); i++){
        buildings.push_back(new QLabel ( tr( vb[i].getName() ) ));
        playersBuildingLayout->addWidget(buildings[i]);
    }
}

void MainWindow::about()
{
    QMessageBox::about(this, tr("About Monopoly"),
    tr("<h2>Economic simulator v0.17</h2>"
    "<p>Copyright &copy; 2010 OMFG! Inc."
    "<p>Monopoly is a small student project "));
}